'use strict'
const path = require('path')
const defaultSettings = require('./src/settings.js')

function resolve(dir) {
  return path.join(__dirname, dir)
}

const name = defaultSettings.title || 'vue Admin Template' // page title

// 对项目端口进行设置
const port = process.env.port || process.env.npm_config_port || 9528 // dev port

module.exports = {

  publicPath: '/',
  outputDir: 'dist',
  assetsDir: 'static',
  lintOnSave: process.env.NODE_ENV === 'development',
  productionSourceMap: false,
  devServer: {
    port: port,
    open: true,
    overlay: {
      warnings: false,
      errors: true
    },
    // proxy只能在开发阶段解决跨域问题,生产环境依然存在跨域,这个跨域需要后台来解决
    // proxy: { // 手写出来!!!!!!!!!
    // // 配置目前项目的请求的基准地址
    // // api是程序员任意配置的,建议和接口前缀保持一致
    // // 当请求地址以/api开头,就会自动触发代理转发机制
    //   '/dev_api': {
    //   // target指目标服务器,向哪一个地址发请求地址
    //   // 当设置好target以后,当发起请求时,请求地址会拼接到target后面

    //     target: 'http://82.157.119.9:3000/',
    //     changeOrigin: true, // 是否跨域
    //     pathRewrite: { // 路径重写
    //       '/dev_api': '/api'
    //     }
    //   }
    // }
    proxy: {
      // 配置项目的请求基地址
      // '/dev-api' 是程序员任意配置的
      // 当请求地址以 /dev_api 开头, 就会自动触发代理转发机制
      '/dev_api': {
        // target 指目标服务器 , 向哪一个地址中发起请求
        // 当设置好 target 以后 , 当发起请求时 , 请求及地址会拼接到target后面
        // target: 'http://8.131.91.46:6868',
        target: 'http://82.157.119.9:3000/',
        // target: 'http://localhost:3000',

        changeOrigin: true, // 是否跨域

        // 如果设置的基准地址和接口前缀不一致 , 需要路径重写
        // 例如 http://8.131.91.46:6868/yase --> http://8.131.91.46:6868/api
        pathRewrite: {
          // 路径重写 将 '/proxy' 替换为 '/api'
          '/dev_api': '/api'
        }
      }
    }

  },

  configureWebpack: {
    // provide the app's title in webpack's name field, so that
    // it can be accessed in index.html to inject the correct title.
    name: name,
    resolve: {
      alias: {
        '@': resolve('src')
      }
    }
  },
  chainWebpack(config) {
    // it can improve the speed of the first screen, it is recommended to turn on preload
    config.plugin('preload').tap(() => [
      {
        rel: 'preload',
        // to ignore runtime.js
        // https://github.com/vuejs/vue-cli/blob/dev/packages/@vue/cli-service/lib/config/app.js#L171
        fileBlacklist: [/\.map$/, /hot-update\.js$/, /runtime\..*\.js$/],
        include: 'initial'
      }
    ])

    // when there are many pages, it will cause too many meaningless requests
    config.plugins.delete('prefetch')

    // set svg-sprite-loader
    config.module
      .rule('svg')
      .exclude.add(resolve('src/icons'))
      .end()
    config.module
      .rule('icons')
      .test(/\.svg$/)
      .include.add(resolve('src/icons'))
      .end()
      .use('svg-sprite-loader')
      .loader('svg-sprite-loader')
      .options({
        symbolId: 'icon-[name]'
      })
      .end()

    config
      .when(process.env.NODE_ENV !== 'development',
        config => {
          config
            .plugin('ScriptExtHtmlWebpackPlugin')
            .after('html')
            .use('script-ext-html-webpack-plugin', [{
            // `runtime` must same as runtimeChunk name. default is `runtime`
              inline: /runtime\..*\.js$/
            }])
            .end()
          config
            .optimization.splitChunks({
              chunks: 'all',
              cacheGroups: {
                libs: {
                  name: 'chunk-libs',
                  test: /[\\/]node_modules[\\/]/,
                  priority: 10,
                  chunks: 'initial' // only package third parties that are initially dependent
                },
                elementUI: {
                  name: 'chunk-elementUI', // split elementUI into a single package
                  priority: 20, // the weight needs to be larger than libs and app or it will be packaged into libs or app
                  test: /[\\/]node_modules[\\/]_?element-ui(.*)/ // in order to adapt to cnpm
                },
                commons: {
                  name: 'chunk-commons',
                  test: resolve('src/components'), // can customize your rules
                  minChunks: 3, //  minimum common number
                  priority: 5,
                  reuseExistingChunk: true
                }
              }
            })
          // https:// webpack.js.org/configuration/optimization/#optimizationruntimechunk
          config.optimization.runtimeChunk('single')
        }
      )
  }
}
